package org.htmlparser.parserapplications.filterbuilder;

import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Insets;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintStream;
import java.util.Hashtable;
import java.util.Vector;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.EtchedBorder;
import javax.swing.border.LineBorder;
import org.htmlparser.NodeFilter;
import org.htmlparser.Parser;
import org.htmlparser.parserapplications.filterbuilder.layouts.VerticalLayoutManager;

public abstract class Filter extends JComponent
  implements NodeFilter
{
  protected static Hashtable mWrappers = new Hashtable();

  public Filter()
  {
    setToolTipText(getDescription());
    setLayout(new VerticalLayoutManager());
    setSelected(false);
    JLabel localJLabel = new JLabel(getDescription(), getIcon(), 2);
    localJLabel.setBackground(Color.green);
    localJLabel.setAlignmentX(0F);
    localJLabel.setHorizontalAlignment(2);
    add(localJLabel);
    Dimension localDimension = localJLabel.getMaximumSize();
    Insets localInsets = getInsets();
    localDimension.setSize(localDimension.width + localInsets.left + localInsets.right, localDimension.height + localInsets.top + localInsets.bottom);
    setSize(localDimension);
  }

  public static String deconstitute(Filter[] paramArrayOfFilter)
    throws IOException
  {
    StringBuffer localStringBuffer = new StringBuffer(1024);
    for (int i = 0; i < paramArrayOfFilter.length; ++i)
    {
      localStringBuffer.append("[");
      localStringBuffer.append(serialize(pickle(paramArrayOfFilter[i].getNodeFilter())));
      localStringBuffer.append("]");
    }
    return localStringBuffer.toString();
  }

  public static byte[] deserialize(String paramString)
  {
    byte[] arrayOfByte = new byte[paramString.length() / 2];
    for (int i = 0; i < paramString.length(); i += 2)
      arrayOfByte[(i / 2)] = (byte)Integer.parseInt(paramString.substring(i, i + 2), 16);
    return arrayOfByte;
  }

  protected static SubFilterList getEnclosed(Component paramComponent)
  {
    SubFilterList localSubFilterList;
    int i;
    if (paramComponent instanceof Container)
    {
      Component[] arrayOfComponent = ((Container)paramComponent).getComponents();
      i = 0;
      if (i < arrayOfComponent.length)
        if (arrayOfComponent[i] instanceof SubFilterList)
          localSubFilterList = (SubFilterList)arrayOfComponent[i];
    }
    while (true)
    {
      while (true)
      {
        return localSubFilterList;
        ++i;
      }
      localSubFilterList = null;
    }
  }

  public static Filter instantiate(String paramString)
  {
    Filter localFilter = null;
    try
    {
      localFilter = (Filter)Class.forName(paramString).newInstance();
      mWrappers.put(localFilter.getNodeFilter().getClass().getName(), paramString);
      return localFilter;
    }
    catch (ClassNotFoundException localClassNotFoundException)
    {
      System.out.println("can't find class " + paramString);
    }
    catch (InstantiationException localInstantiationException)
    {
      System.out.println("can't instantiate class " + paramString);
    }
    catch (IllegalAccessException localIllegalAccessException)
    {
      System.out.println("class " + paramString + " has no public constructor");
    }
    catch (ClassCastException localClassCastException)
    {
      System.out.println("class " + paramString + " is not a Filter");
    }
  }

  public static void newline(StringBuffer paramStringBuffer)
  {
    paramStringBuffer.append('\n');
  }

  public static byte[] pickle(Object paramObject)
    throws IOException
  {
    ByteArrayOutputStream localByteArrayOutputStream = new ByteArrayOutputStream();
    ObjectOutputStream localObjectOutputStream = new ObjectOutputStream(localByteArrayOutputStream);
    localObjectOutputStream.writeObject(paramObject);
    localObjectOutputStream.close();
    return localByteArrayOutputStream.toByteArray();
  }

  public static Filter[] reconstitute(String paramString, Parser paramParser)
  {
    Vector localVector = new Vector();
    try
    {
      Filter localFilter;
      do
      {
        if (!(paramString.startsWith("[")))
          break label98;
        int i = paramString.indexOf(93);
        if (-1 == i)
          break label98;
        String str = paramString.substring(1, i);
        paramString = paramString.substring(i + 1);
        Object localObject = unpickle(deserialize(str));
        if (!(localObject instanceof NodeFilter))
          break label98;
        localFilter = wrap((NodeFilter)localObject, paramParser);
      }
      while (localFilter == null);
      label98: localVector.addElement(localFilter);
    }
    catch (Exception localException)
    {
      localException.printStackTrace();
      Filter[] arrayOfFilter = new Filter[localVector.size()];
      localVector.copyInto(arrayOfFilter);
      return arrayOfFilter;
    }
  }

  public static String serialize(byte[] paramArrayOfByte)
  {
    StringBuffer localStringBuffer = new StringBuffer(2 * paramArrayOfByte.length);
    for (int i = 0; i < paramArrayOfByte.length; ++i)
    {
      String str = Integer.toString(0xFF & paramArrayOfByte[i], 16);
      if (str.length() < 2)
        localStringBuffer.append("0");
      localStringBuffer.append(str);
    }
    return localStringBuffer.toString();
  }

  public static void spaces(StringBuffer paramStringBuffer, int paramInt)
  {
    for (int i = 0; i < paramInt; ++i)
      paramStringBuffer.append(' ');
  }

  public static Object unpickle(byte[] paramArrayOfByte)
    throws IOException, ClassNotFoundException
  {
    ObjectInputStream localObjectInputStream = new ObjectInputStream(new ByteArrayInputStream(paramArrayOfByte));
    Object localObject = localObjectInputStream.readObject();
    localObjectInputStream.close();
    return localObject;
  }

  public static Filter wrap(NodeFilter paramNodeFilter, Parser paramParser)
  {
    Filter localFilter = null;
    String str1 = paramNodeFilter.getClass().getName();
    String str2 = (String)mWrappers.get(str1);
    if (str2 != null);
    try
    {
      localFilter = instantiate(str2);
      localFilter.setNodeFilter(paramNodeFilter, paramParser);
      NodeFilter[] arrayOfNodeFilter = localFilter.getSubNodeFilters();
      if (arrayOfNodeFilter.length != 0)
      {
        SubFilterList localSubFilterList = getEnclosed(localFilter);
        if (localSubFilterList != null)
        {
          localFilter.setSubNodeFilters(new NodeFilter[0]);
          int i = 0;
          while (true)
          {
            if (i >= arrayOfNodeFilter.length)
              break label119;
            localSubFilterList.addFilter(wrap(arrayOfNodeFilter[i], paramParser));
            ++i;
          }
        }
        label119: throw new IllegalStateException("filter can't have subnodes without a SubFilterList on the wrapper");
      }
    }
    catch (Exception localException)
    {
      localException.printStackTrace();
      return localFilter;
      System.out.println(str2 + " is not registered for wrapping.");
    }
  }

  public abstract String getDescription();

  public Icon getIcon()
  {
    Object localObject = null;
    try
    {
      ImageIcon localImageIcon = new ImageIcon(getClass().getResource(getIconSpec()));
      localObject = localImageIcon;
      return localObject;
    }
    catch (NullPointerException localNullPointerException)
    {
      System.err.println("can't find icon " + getIconSpec());
    }
  }

  public abstract String getIconSpec();

  public abstract NodeFilter getNodeFilter();

  public abstract NodeFilter[] getSubNodeFilters();

  public void setExpanded(boolean paramBoolean)
  {
    Component[] arrayOfComponent = getComponents();
    for (int i = 0; i < arrayOfComponent.length; ++i)
      if (!(arrayOfComponent[i] instanceof JLabel))
        arrayOfComponent[i].setVisible(paramBoolean);
  }

  public abstract void setNodeFilter(NodeFilter paramNodeFilter, Parser paramParser);

  public void setSelected(boolean paramBoolean)
  {
    if (paramBoolean)
      setBorder(new CompoundBorder(new EtchedBorder(), new CompoundBorder(new LineBorder(Color.blue, 2), new EmptyBorder(1, 1, 1, 1))));
    while (true)
    {
      return;
      setBorder(new CompoundBorder(new EtchedBorder(), new EmptyBorder(3, 3, 3, 3)));
    }
  }

  public abstract void setSubNodeFilters(NodeFilter[] paramArrayOfNodeFilter);

  public abstract String toJavaCode(StringBuffer paramStringBuffer, int[] paramArrayOfInt);

  public String toString()
  {
    return getDescription() + " [" + getClass().getName() + "]";
  }
}